package com.fy.tt;

import com.fy.tt.executor.node.script.Script;
import com.fy.tt.parser.BandLanguageLexer;
import com.fy.tt.parser.BandLanguageParser;
import com.fy.tt.parser.MyBandLanguageVisitor;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.TokenSource;
import org.antlr.v4.runtime.tree.ParseTree;

import java.util.HashMap;
import java.util.Map;

public class test {
    public static void main(String[] args) {
        runScript(" 张三在喇叭帮区执行喇叭工具的发声部件。","001",new HashMap<String,String>(),"");
    }

    public static boolean runScript(String script, String bandId, Map env, String accessToken) {
//        String script = "如果 B8 有 生 ，则 B8 警报 。";
        CharStream input = CharStreams.fromString(script);
        System.out.println("该剧本为:"+script);


        long startTime = System.currentTimeMillis();

        //词法分析
        BandLanguageLexer lexer = new BandLanguageLexer(input);
        CommonTokenStream tokens = new CommonTokenStream((TokenSource) lexer);
        System.out.println(tokens.getNumberOfOnChannelTokens());

        for (int i = 1; i <= tokens.getNumberOfOnChannelTokens(); i++) {
            System.out.println(tokens.LT(i).getText() + ":" + tokens.LA(i));
        }
        long endTime = System.currentTimeMillis();
        System.out.println("词法程序运行时间：" + (endTime - startTime) + "ms");
        long startTime1 = System.currentTimeMillis();
        //语法分析

//
        BandLanguageParser parser = new BandLanguageParser(tokens);
        ParseTree tree = parser.script();

        System.out.println("展示树:");
        long endTime1 = System.currentTimeMillis();
        System.out.println("语法程序运行时间：" + (endTime1 - startTime1) + "ms");

        MyBandLanguageVisitor myVisitor = new MyBandLanguageVisitor();

        Script text = (Script) myVisitor.visit(tree);
        boolean res = text.run("");
        return res;
    }
}
